<?php 
class news extends MY_Controller {
		function __construct(){
			parent::__construct();
		}

		function admin_index(){
			$this->load->model('news_model','newsModel');
			$b = $this->newsModel->get();
			$this->_assign('news',$b);
			$this->load->model('images_model','imagesModel');
			$img = $this->imagesModel->get();
			$this->_assign('imgs',$img);
			$this->_fetch('admin_list');
		}

		function admin_form(){
			$this->_fetch("admin_add");
		}

		function admin_add($id=""){
			if(!empty($id)){
        $reslut = $this->newsModel->get($id);
				$this->_assign('new',$reslut);
       	$this->load->model('images_model','imagesModel');
				$whereImage["where"]["pos_id"] = $id;
				$imgs = $this->imagesModel->get($whereImage);
				$this->_assign('imgs',$imgs);
        $this->_fetch('admin_add');
			}else {
				$this->load->model("news_model","newsModel");
				$_post=$this->input->post();
		
				$this->load->library('upload');
		 		$config['upload_path'] = 'resource/image';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '1024';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				$config['encrypt_name'] = 'true';
				$this->upload->initialize($config);
				$totlecount = count($_FILES['filename']['name']);
				for($i=0; $i<$totlecount; $i++){
					$_FILES['userfile']['name']    = $_FILES['filename']['name'][$i];
					$_FILES['userfile']['type']    = $_FILES['filename']['type'][$i];
					$_FILES['userfile']['tmp_name'] = $_FILES['filename']['tmp_name'][$i];
	 				$_FILES['userfile']['error']       = $_FILES['filename']['error'][$i];
	 				$_FILES['userfile']['size']    = $_FILES['filename']['size'][$i];

					if (!$this->upload->do_upload()){
						echo $this->upload->display_errors();
					}else{
						$picturesData[] = $this->upload->data();
					}
				}


				$_post["status"] = 1;
				$_post["url"] = Util::url_title($_post["title"]);
				$_post["usr_id"] = $this->session->userdata("user_id");

				$post_id = $this->newsModel->add($_post);


				if($_post['img_id']){
					
					$this->load->model('images_model','imagesModel');
					$where2['where']['pos_id'] = $_post['id'];
					$where2['where']['firstimage'] = 1;
					$myimg= $this->imagesModel->get($where2);
					
					if($myimg[0]['firstimage']==1){
					$add1['id'] = $myimg[0]['id'];
					$add1['firstimage'] = 0;
					$b=$this->imagesModel->add($add1);
											
					}

					$firstimage['id'] = $_post['img_id'];
					$firstimage['firstimage'] = 1;
					$this->imagesModel->add($firstimage);

					}


				if($post_id){
					 $i=1;// 10/8/2556 check
			 		foreach($picturesData as $picture) {
			 			$this->load->model('images_model','imagesModel');
			 				$b['filename'] = $picture['file_name'];
			 				$b['pos_id'] = $post_id;

			 				// 10/8/2556 check
			 				if($i==1){
			 					$b['firstimage'] = 1;
			 				}else{
			 					$b['firstimage'] = 0;	
			 				}
			 				//

			 				$this->imagesModel->add($b);

			 				$i++;// 10/8/2556 check	
			 		}
					
				}

				redirect(base_url('news/admin_index'));

			}
}


		function admin_delete($id=""){

			 //$this->load->model('news_model','newsMOdel');

			$where["where"]["id"] = $id;
			$get=$this->newsModel->get($where);

			if($get[0]['status']==1){
				$add['id'] = $get[0]['id'];
				$add['status'] = 0;

				$this->newsModel->add($add);
			}

		 redirect(base_url('news/admin_index'));


			/*
			$this->db->where('pos_type','news');
			$this->db->delete('ci_post',array('pos_id'=>$id));
			redirect('post');
			*/
		}


		function admin_content($url=""){

		$where['where']['url'] = urldecode($url);
		$reslut = $this->newsModel->get($where);
		$this->_assign('news',$reslut);

		$this->load->model('images_model','imagesModel');
		$img['where']['pos_id'] = $reslut[0]['id'];
		$imgs=$this->imagesModel->get($img);
		$this->_assign("imgs",$imgs);

		$this->_fetch('admin_view');


		}

	}
?>